<template>
  <el-form :inline="true" :model="cForm" class="demo-form-inline" :disabled="!isShowCard">
    <el-form-item label="一级分类">
      <el-select
        v-model="cForm.category1Id"
        placeholder="请选择"
        @change="updateCategory1"
      >
        <el-option
          :label="category1.name"
          :value="category1.id"
          v-for="category1 in category1List"
          :key="category1.id"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="二级分类">
      <el-select
        v-model="cForm.category2Id"
        placeholder="请选择"
        @change="updateCategory2"
      >
        <el-option
          :label="c2.name"
          :value="c2.id"
          v-for="c2 in category2List"
          :key="c2.id"
        ></el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="三级分类">
      <el-select
        v-model="cForm.category3Id"
        placeholder="请选择"
        @change="updateCategory3"
      >
        <el-option
          :label="c3.name"
          :value="c3.id"
          v-for="c3 in category3List"
          :key="c3.id"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  name: "",
  props:["isShowCard"],
  data() {
    return {
      cForm: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
      category1List: [],
      category2List: [],
      category3List: [],
    };
  },
  mounted() {
    this.getCategory1List();
  },
  methods: {
    async getCategory1List() {
      try {
        let result = await this.$API.categoryselector.getCategory1();
        if (result.code === 200) {
          this.category1List = result.data;
        }
      } catch (error) {
        this.$message.error("网络链接超时！请刷新页面");
      }
    },
    async updateCategory1() {
      this.cForm.category2Id = "";
      this.cForm.category3Id = "";
      this.category2List = [];
      this.category3List = [];
      try {
        let { category1Id } = this.cForm;
        if (category1Id) {
          this.$emit("changeCategory", { categoryId: category1Id, lveal: 1 });
          let result = await this.$API.categoryselector.getCategory2(
            category1Id
          );
          if (result.code === 200) {
            this.category2List = result.data;
          }
        }
      } catch (error) {
        this.$message.error("网络链接超时！请刷新页面");
      }
    },
    async updateCategory2() {
      this.cForm.category3Id = "";
      this.category3List = [];
      try {
        let { category2Id } = this.cForm;
        if (category2Id) {
          this.$emit("changeCategory", { categoryId: category2Id, lveal: 2 });
          let result = await this.$API.categoryselector.getCategory3(
            category2Id
          );
          if (result.code === 200) {
            this.category3List = result.data;
          }
        }
      } catch (error) {
        this.$message.error("网络链接超时！请刷新页面");
      }
    },
    updateCategory3() {
      let { category3Id } = this.cForm;

      this.$emit("changeCategory", { categoryId: category3Id, lveal: 3 });
    },
  },
};
</script>

<style>
</style>